package com.EthanYankang;

import java.io.IOException;
import java.io.OutputStream;
import java.net.Socket;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: Ethan Yankang
 * @Program: my-tomcat2
 * @Date: 2024-09-09 14:49
 **/
public class Response extends AbstractHttpResponse{
    private int status=200;
    private String message="OK";
    private Map<String,String> headers =new HashMap<>();//存放我们自己要写的返回的响应头

    private Request request;
    //响应的格式
    private final static byte SP=' ';
    private final static byte  CR='\r';
    private final static byte LF='\n';
    private OutputStream socketOutputStream;
    private ResponseServletOutputStream responseServletOutputStream = new ResponseServletOutputStream();



    //一个请求对应一个连接
    public Response(Request request) throws IOException {
        this.request = request;
        this.socketOutputStream=request.getSocket().getOutputStream();
    }


    public Response(int status, String message, Map<String, String> headers, Socket socket, Request request) {
        this.status = status;
        this.message = message;
        this.headers = headers;
    }
    @Override
    public void setStatus(int i) {
        this.status=i;
    }

    @Override
    public void addHeader(String s, String s1) {
        headers.put(s, s1);
    }

        @Override
    public void setStatus(int i, String s) {
        this.status=i;
        this.message=s;
    }

    @Override
    public ResponseServletOutputStream getOutputStream() throws IOException {
        return responseServletOutputStream;
    }

    public void complete() throws IOException {
        sendResponseLine();//发送响应行
        sendResponseHeader();//发送响应头
        sendResponseBody();//发送响应体

        System.out.println("响应完成");
    }
    private void sendResponseLine() throws IOException {
        socketOutputStream.write(request.getProtocol().getBytes());
        socketOutputStream.write(SP);
        socketOutputStream.write(status);
        socketOutputStream.write(SP);
        socketOutputStream.write(message.getBytes());
        socketOutputStream.write(CR);
        socketOutputStream.write(LF);
    }

    private void sendResponseHeader() throws IOException {
        if (!headers.containsKey("Content-Length")) {
            addHeader("Content-Length", String.valueOf(responseServletOutputStream.getPos()));
        }
        if (!headers.containsKey("Content-Type")) {
            addHeader("Content-Type", "text/plain;charset=utf-8");
        }

        //发送每一行响应体
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            socketOutputStream.write(entry.getKey().getBytes());
            socketOutputStream.write(":".getBytes());
            socketOutputStream.write(entry.getValue().getBytes());
            socketOutputStream.write(CR);
            socketOutputStream.write(LF);
        }
        //响应体发送结束
        socketOutputStream.write(CR);
        socketOutputStream.write(LF);
    }


    private void sendResponseBody() throws IOException {
        //这就是我们自己的响应
        socketOutputStream.write(this.getOutputStream().getBytes());
    }

}
